var id=$('#providerId').val();

$(function(){
	$('#name').focus();
	initRadioStyle();
	setMyActive(6,1); //设置激活页
	chcekDataType();
});

//加载i-checks类样式
function initRadioStyle(){  
    $('.i-checks').iCheck({
        checkboxClass: 'icheckbox_square-green',
        radioClass: 'iradio_square-green',
    });
    bindDataTypeEvent();
    bindProvinceCodeEvent();
}

//初始化单选钮单击事件,在插件初始化之前绑定 
function bindDataTypeEvent(){
	$('input[name=dataType]').on('ifChecked', function(event){
		chcekDataType();
	});
}

//初始化单选钮单击事件,在插件初始化之前绑定 
function bindProvinceCodeEvent(){
	$('input[name=provinceCode]').on('ifChecked', function(event){
		dataPkgList();
	});
}

function chcekDataType(){
	var dataType = $("input[name='dataType']:checked").val();
	if(dataType=='2'){
		initNation();
	}else{
		initProvince();
	}
}

function initNation(){
	$('#dataPkg').empty();
	var dataDiv='province';
	var myDiscountDiv='';
	myDiscountDiv+='<div class="col-sm-3 form-group ">';
	myDiscountDiv += '	<label > 01 <input class="i-checks" type="radio" '+
					 ' name="provinceCode" value="全国" > <i></i>全国</label>';
	myDiscountDiv+='</div>';
	
	$('#'+dataDiv).empty();
	$('#'+dataDiv).append(myDiscountDiv);
	$.each(provinceCityData,function (index,el){
		myDiscountDiv='';
		myDiscountDiv+='<div class="col-sm-3 form-group ">';
		if(index < 8){
			myDiscountDiv +='	<label > 0'+(index+2)+' <input class="i-checks" type="radio" '+
							' name="provinceCode" value="'+el.name+'" > <i></i>'+el.name+'</label>';
		}else{
			myDiscountDiv +='	<label > '+(index+2)+' <input class="i-checks" type="radio" '+
		 					' name="provinceCode"  value="'+el.name+'"> <i></i>'+el.name+'</label>';
		}
		myDiscountDiv+='</div>';
		$('#'+dataDiv).append(myDiscountDiv);
	});
	initRadioStyle();
}

function initProvince(){
	$('#dataPkg').empty();
	var dataDiv='province';
	$('#'+dataDiv).empty();
	$.each(provinceCityData,function (index,el){
		myDiscountDiv='';
		myDiscountDiv+='<div class="col-sm-3 form-group ">';
		if(index < 8){
			myDiscountDiv +='	<label > 0'+(index+1)+' <input class="i-checks" type="radio" '+
							' name="provinceCode" value="'+el.name+'"> <i></i>'+el.name+'</label>';
		}else{
			myDiscountDiv +='	<label > '+(index+1)+' <input class="i-checks" type="radio" '+
		 					' name="provinceCode" value="'+el.name+'"> <i></i>'+el.name+'</label>';
		}
		myDiscountDiv+='</div>';
		$('#'+dataDiv).append(myDiscountDiv);
	});
	initRadioStyle();
}

function dataPkgList(){
	$('#dataPkg').empty();
	var dataType = $("input[name='dataType']:checked").val();
	$.ajax({
		type:'GET',
		url:basePath+'dataPkgList',
		data:{
			dataType:dataType
		},
		async:false,
		dataType:'json',
		success:function(data){
			$.each(data,function (index,el){
				myDiscountDiv='';
				myDiscountDiv+='<div class="col-sm-3 form-group ">';
				if(index < 8){
					myDiscountDiv +='	<label > 0'+(index+1)+' <input class="i-checks" type="checkbox" '+
									'id="'+el.id+'" name="dataPkgId"> <i></i>'+el.name+'</label>';
				}else{
					myDiscountDiv +='	<label > '+(index+1)+' <input class="i-checks" type="checkbox" '+
				 					'id="'+el.id+'" name="dataPkgId"> <i></i>'+el.name+'</label>';
				}
				myDiscountDiv+='</div>';
				$('#dataPkg').append(myDiscountDiv);
			});
			initRadioStyle();
		}
	});
	checkDataPkg();
}

function checkDataPkg(){
	var dataType = $("input[name='dataType']:checked").val();
	var provinceCode = $("input[name='provinceCode']:checked").val();
	$.ajax({
		type:'GET',
		url:basePath+'providerDataPkgGet',
		data:{
			providerId:id,
			dataType:dataType,
			provinceCode:provinceCode
		},
		success:function(data){
			$.each(data, function(index, val) {
				$("#"+val.id).iCheck('check');
			});
		},
		dataType:'json'
	});
}

function saveUpdate(){
	var provinceCode = $("input[name='provinceCode']:checked").val();
	if(provinceCode==''||provinceCode==null){
		swal({
	        title: "请选择省份",
	        type: "success",
	        confirmButtonText: "ok"
		});
	 return false;
	}
	 var obj=document.getElementsByName('dataPkgId');
	 var selectedMeg='';
	 var uncheckedMeg='';
	 for(var i=0; i<obj.length; i++){    
		    if(obj[i].checked) {//如果选中，将value添加到变量s中 
		    	var s=obj[i].id;  
		    	selectedMeg+=s+',';
		    }else{
		    	var s=obj[i].id;  
		    	uncheckedMeg+=s+',';
		    }
	 	}    
	
	$.ajax({
		type:'PUT',
		url :basePath +'providerDataPkgEdit/'+id,
		data:{ 
			provinceCode:provinceCode,
			selectedMeg:selectedMeg,
			uncheckedMeg:uncheckedMeg
		},
		success:function (dt){
			if(dt){
				swal("友情提示", "更新流量包成功！", "success");
			}else{
				swal("友情提示", "更新流量包失败,", "error");
			}
	    },
	    dataType: 'html'
	});
}
